//
//  WBNewFeature.swift
//  新浪微博
//
//  Created by 张甜甜 on 2017/11/29.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
//版本更新时显示
class WBNewFeature: UIView {
    private lazy var scrollView:UIScrollView = UIScrollView()
    private lazy var enterButton:UIButton = UIButton()
    private lazy var pageControl:UIPageControl = UIPageControl()
    override init(frame: CGRect) {
        super .init(frame: frame)
        backgroundColor = UIColor .orange
        setUpUi()
        setData()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("错误信息")
    }
    
    
    
}
extension WBNewFeature : UIScrollViewDelegate{
    private func setUpUi(){
       self .backgroundColor = UIColor .clear
        scrollView.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height)
        scrollView .isPagingEnabled = true
        scrollView .showsHorizontalScrollIndicator = false
        scrollView .bounces = false
        scrollView .delegate = self
        scrollView .backgroundColor = UIColor .clear
        self.addSubview(scrollView)
        
        enterButton.frame = CGRect(x: (ScreenWidth-70)/2, y: ScreenHeight-140, width: 70, height: 25)
        enterButton .setBackgroundImage(UIImage(named: "new_feature_finish_button"), for: .normal)
        enterButton .setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), for: .selected)
        enterButton .titleLabel?.font = UIFont .systemFont(ofSize: 15)
        enterButton .setTitle("进入微博", for: .normal)
        enterButton .addTarget(self, action: #selector(bottomClick), for: .touchUpInside)
        enterButton .isHidden = true
        self.addSubview(enterButton)
        
        pageControl.frame = CGRect(x: enterButton.frame.origin.x, y: enterButton.frame.origin.y + enterButton.frame.size.height + 10, width: enterButton.frame.size.width, height: 20)
        pageControl.pageIndicatorTintColor = UIColor .black
        pageControl.currentPageIndicatorTintColor = UIColor .orange
        pageControl.numberOfPages = 4
        self .addSubview(pageControl)
    }
    @objc private func bottomClick(){
        print("点击")
        self .removeFromSuperview()
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //一旦滚动隐藏按钮
        enterButton .isHidden = true
        let page = Int((scrollView .contentOffset.x) / (scrollView.bounds.width + 0.5))
        pageControl .isHidden = (page == 3)
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = (scrollView .contentOffset.x)/self.bounds.width
        pageControl .currentPage = Int(page)
        if Int(page) == 4 {
            self .removeFromSuperview()
        }
        enterButton .isHidden = (Int(page) != 3)
    }
}
extension WBNewFeature {
    private func setData(){
        //添加四个图像视图
        let  count  = 4
        for i in 0..<count {
            let imageName:String = "new_feature_" + "\(i+1)"
            let iv : UIImageView = UIImageView(frame: CGRect(x: self.bounds.width * CGFloat(i), y: 0, width: self.bounds.width , height: self.bounds.height))
            iv .image = UIImage(named: imageName)
            scrollView .addSubview(iv)
        }
        scrollView .contentSize = CGSize(width: CGFloat(count + 1) * self.bounds.width , height: self.bounds.height)
        
    }
}






